﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Util;
public partial class newsList : System.Web.UI.Page
{
    private int PageIndex { get { return (int)ViewState["index"]; } set { ViewState["index"] = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            PageIndex = 1;
            LoadView();
            LoadData();
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "新闻列表";
        new BllNewstype().InitDdl(ddlType);
    }

    private Func<tb_news, bool> GetFunc()
    {
        var func = UtilExp.ExprTrue<tb_news>();
         if (!ddlType.SelectedValue.Equals("0")) 
         {
            func= func.ExprAnd(c => c.type == int.Parse(this.ddlType.SelectedValue));
         }
         return func.Compile();

    }

    private void LoadData()
    {
        BllNews bll = new BllNews();
        
        var func = GetFunc();
        webPager.RecordCount = bll.GetCount(func);

        var tp = bll.GetList(func,true,c=>c.time,PageIndex, webPager.PageSize);

        var data = from c in tp
                   select new
                   {
                       id = c.id,
                       name = UtilStr.CutStr(c.name, 14),
                       type = new BllNewstype().GetSingle(c.type).name,
                     
                   };
        dl.DataSource = data;
        dl.DataBind();
    }

    protected void webPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        PageIndex = e.NewPageIndex;
        LoadData();
    }

    protected void dl_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string cmd = e.CommandName;
        int id = Convert.ToInt32(e.CommandArgument);
        BllNews bll = new BllNews(this);
        if (cmd == "edit")
        {
            Response.Redirect("newsEdit.aspx?id=" + id.ToString());
        }
        if (cmd == "delete")
        {
            if (bll.Delete(id))
                LoadData();
        }
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData();
    }
}